﻿using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using System;
using NB.MGJ.Model.AntiFake;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Common;

namespace NB.MGJ.Service.Origins
{
    public partial class OriginsTempletService
    {
        private BaseRepository _baseRepository = new BaseRepository();

        #region  增

        public int Insert(Origins_Templet model)
        {
            return _baseRepository.InsertReturnID<Origins_Templet>(model);
        }

        #endregion

        #region  删

        public int Del(int Id)
        {
            string sql = "update origins_templet  set ViewDel=1 where TempletId=" + Id;
            var result = _baseRepository.ExecuteSql<int>(sql);
            return result;
        }

        #endregion

        #region  改
        public int Update(Origins_Templet model)
        {
            int result = _baseRepository.Update<Origins_Templet>(model);
            return result > 0 ? model.TempletId : 0;
        }

        #endregion

        #region  查

        public Origins_Templet GetModelById(int TempletId)
        {
            string sql = "select t.*,c.ClassifyName from origins_templet t left join product_classify c on t.ClassifyId=c.ID where t.TempletId=" + TempletId;
            return _baseRepository.ExecuteSqlCollection<Origins_Templet>(sql).FirstOrDefault();
        }

        public PagedList<Origins_Templet> GetPageList(ref PagerModel pager)
        {
            try
            {

                StringBuilder strSql1 = new StringBuilder();
                strSql1.Append(@" Origins_Templet ");
                var strSql = new StringBuilder(strSql1.ToString().Trim(','));
                pager.FldName = " * ";
                pager.TblName = strSql.ToString();

                if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == "")
                    pager.FldSort = "  ChangeDate desc ";

                MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);
                IEnumerable<Origins_Templet> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Templet>("Common_PageList", parameters);
                return new PagedList<Origins_Templet>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public PagedList<Origins_Templet> GetTemplateList(ref PagerModel pager)
        {
            StringBuilder strSql1 = new StringBuilder();
            strSql1.Append("Origins_Templet.*,");
            strSql1.Append(" (select ProductName from Product_Product where Origins_Templet.TempletId=Product_Product.ID  ) as ProductName,");
            strSql1.Append(" (select ClassifyName from Product_Classify where Origins_Templet.ClassifyId=Product_Classify.ID  ) as ClassifyName ");
            pager.FldName = strSql1.ToString();
            pager.TblName = " Origins_Templet ";
            MySqlParameter[] parameters = _baseRepository.SqParameterRefactor(ref pager);
            IEnumerable<Origins_Templet> resultList = _baseRepository.ExecuteStoredProcedureCollection<Origins_Templet>("Common_PageList", parameters).ToList();
            return new PagedList<Origins_Templet>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
        }

        #endregion


    }
}